package org.yczbj.ycvideoplayer.ability.ad;

import ohos.aafwk.ability.Ability;
import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import com.yc.video.bridge.ControlWrapper;
import com.yc.video.config.ConstantKeys;
import com.yc.video.tool.BaseToast;
import com.yc.video.tool.PlayerUtils;
import com.yc.video.ui.view.InterControlView;

import org.yczbj.ycvideoplayer.ResourceTable;

/**
 * AdControlView
 *
 * @since 2021-05-12
 */
public class AdControlView extends StackLayout implements InterControlView, Component.ClickedListener {
    /**
     * 广告时间
     */
    protected Text mAdTime;
    /**
     * 广告详情
     */
    protected Text mAdDetail;
    /**
     * 返回
     */
    protected Image mBack;
    /**
     * 音量
     */
    protected Image mVolume;
    /**
     * 全屏
     */
    protected Image mFullScreen;
    /**
     * 播放按钮
     */
    protected Image mPlayButton;
    /**
     * 广告控制侦听器
     */
    protected AdControlListener mListener;
    private ControlWrapper mControlWrapper;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public AdControlView(Context context) {
        super(context);
        init(context);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrs 自定义属性
     */
    public AdControlView(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrs 自定义属性
     * @param defStyleAttr 默认主题风格
     */
    public AdControlView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_layout_ad_control_view, this, true);
        mAdTime = (Text) findComponentById(ResourceTable.Id_ad_time);
        mAdDetail = (Text) findComponentById(ResourceTable.Id_ad_detail);
        mAdDetail.setText("了解详情>");
        mBack = (Image) findComponentById(ResourceTable.Id_back);
        mBack.setVisibility(HIDE);
        mVolume = (Image) findComponentById(ResourceTable.Id_iv_volume);
        mFullScreen = (Image) findComponentById(ResourceTable.Id_fullscreen);
        mPlayButton = (Image) findComponentById(ResourceTable.Id_iv_play);
        mPlayButton.setClickedListener(this);
        mAdTime.setClickedListener(this);
        mAdDetail.setClickedListener(this);
        mBack.setClickedListener(this);
        mVolume.setClickedListener(this);
        mFullScreen.setClickedListener(this);
        this.setTouchEventListener(new TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
//                BaseToast.showRoundRectToast("广告点击跳转");
                return false;
            }
        });
        this.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (mListener != null) {
                    mListener.onAdClick();
                }
            }
        });
    }

    @Override
    public void attach(ControlWrapper controlWrapper) {
        mControlWrapper = controlWrapper;
    }

    @Override
    public Component getView() {
        return this;
    }

    @Override
    public void onVisibilityChanged(boolean isVisible, Animator anim) {
    }

    @Override
    public void onPlayStateChanged(int playState) {
        switch (playState) {
            case ConstantKeys.CurrentState.STATE_PLAYING:
                mControlWrapper.startProgress();
                mPlayButton.setSelected(true);
                break;
            case ConstantKeys.CurrentState.STATE_PAUSED:
                mPlayButton.setSelected(false);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPlayerStateChanged(int playerState) {
        switch (playerState) {
            case ConstantKeys.PlayMode.MODE_NORMAL:
                mBack.setVisibility(HIDE);
                mFullScreen.setSelected(false);
                break;
            case ConstantKeys.PlayMode.MODE_FULL_SCREEN:
                mBack.setVisibility(VISIBLE);
                mFullScreen.setSelected(true);
                break;
            default:
                break;
        }
        // 暂未实现全面屏适配逻辑，需要你自己补全
    }

    @Override
    public void setProgress(int duration, int position) {
        if (mAdTime != null) {
            mAdTime.setText(String.format("%s | 跳过", (duration - position) / 1000));
        }
    }

    @Override
    public void onLockStateChanged(boolean isLocked) {
    }

    @Override
    public void onClick(Component component) {
        int id = component.getId();
        if (id == ResourceTable.Id_back | id == ResourceTable.Id_fullscreen) {
            toggleFullScreen();
        } else if (id == ResourceTable.Id_iv_volume) {
            doMute();
        } else if (id == ResourceTable.Id_ad_detail) {
            if (mListener != null) {
                mListener.onAdClick();
            }
        } else if (id == ResourceTable.Id_ad_time) {
            if (mListener != null) {
                mListener.onSkipAd();
            }
        } else if (id == ResourceTable.Id_iv_play) {
            mControlWrapper.togglePlay();
        }
    }

    private void doMute() {
        mControlWrapper.setMute(!mControlWrapper.isMute());
        mVolume.setPixelMap(mControlWrapper.isMute()
            ? ResourceTable.Media_ic_player_volume_up
            : ResourceTable.Media_ic_player_volume_off);
    }

    /**
     * 横竖屏切换
     */
    private void toggleFullScreen() {
        Ability slice = PlayerUtils.scanForActivity(getContext());
        mControlWrapper.toggleFullScreen(slice);
    }

    public void setListener(AdControlListener listener) {
        this.mListener = listener;
    }

    /**
     * 广告控制侦听器
     *
     * @since 2021-05-12
     */
    public interface AdControlListener {
        /**
         * 广告点击
         */
        void onAdClick();

        /**
         * 跳过广告
         */
        void onSkipAd();
    }
}
